import { writeFileSync } from "fs"
import { fileURLToPath } from 'node:url';
// import path from 'node:path';
import { dirname, resolve } from "path"
import packageJson from '../../package.json' assert { type: "json" };
// import { CustomUI } from "../lib/main"
// console.log(CustomUI);


const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);
const __dirname = dirname(__filename);

// const writeFile = async (fileName:string, content:string) => {
const writeFile = async (fileName, content) => {
  try {
    await writeFileSync(fileName, content, {encoding: 'utf-8'})
  } catch (err) {
    console.log("【src/utils/index.ts】文件写入失败", err);
  }
}

// package.json
const json = 
`{
  "name": "comptecounter",
  "version": "${packageJson.version}",
  "description": "测试库模式发包",
  "main": "index.ts",
  "directories": {
    "lib": "lib"
  },
  "scripts": {
    "publish": "npm publish --access=public"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}`

writeFile(resolve(__dirname, '../../comptecounter/', 'package.json'), json)

// index.ts
const ts =
`import * as libs from "./lib/main"

// const exportObj = {}
// Object.keys(libs).forEach(funcKey => {
//   if (funcKey !== '__esModule') {
//     const func = libs[funcKey]
//     exportObj[funcKey] = func
//   }
// })

export default libs
// export default exportObj
`
// let ts = `import {`
// let ept = `export {`
// Object.keys(libs).forEach(funcKey => {
//   ts += `${funcKey},`
//   ept += `${funcKey},`
// })
// ts += `} from "./lib/main"`
// ts += ept

writeFile(resolve(__dirname, '../../comptecounter/', 'index.ts'), ts)